
package com.kugou.game.sdk.ui.widget;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kugou.game.sdk.core.FolderManager;
import com.kugou.game.sdk.core.VipManager.GSInfo;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.AsyncImageLoader;
import com.kugou.game.sdk.utils.AsyncImageLoader.ImageCallback;
import com.kugou.game.sdk.utils.Rx;
import com.kugou.game.sdk.utils.ToastUtil;

/**
 * 描述:VIP客服对话框(横屏)
 *
 * @author jerryliu
 * @since 2016年1月19日 下午4:41:23
 */
public class VipDialogLand extends android.app.Dialog implements ImageCallback {
    private Context context;

    private ImageView gsImageGirl, gsImageClose;

    private ListView gsList;

    private AsyncImageLoader imageLoader;

    public VipDialogLand(Context context) {
        super(context, Rx.style.kg_KYTheme_Widget_Dialog);
        this.context = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(true);
        setCancelable(true);

        initViews(context);
        imageLoader = new AsyncImageLoader();
    }

    private void initViews(final Context context) {
        View view = LayoutInflater.from(context).inflate(Rx.layout.kg_dialog_vip_layout, null);
        gsImageClose = (ImageView) view.findViewById(Rx.id.gs_close);
        gsImageGirl = (ImageView) view.findViewById(Rx.id.gs_image);
        gsList = (ListView) view.findViewById(Rx.id.gs_qq_list);
        gsImageClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowing()) {
                    dismiss();
                }
            }
        });
        setContentView(view);
        getWindow().setLayout(AppUtil.dip2px(context, 349), AppUtil.dip2px(context, 223));
    }

    public void setGsListData(ArrayList<GSInfo> data) {
        // 设置客服信息， 默认显示第一个
        QQlistAdapter adapter = new QQlistAdapter(context, data, 0);
        gsList.setAdapter(adapter);
        if (data != null) {
            GSInfo info = data.get(0);
            if (info != null) {
                loadGsImage(info, 0);
            }
        }
    }

    class QQlistAdapter extends BaseAdapter {
        ArrayList<GSInfo> data;

        Context context;

        LayoutInflater inflater;

        int selectedPosition;

        public QQlistAdapter(Context context, ArrayList<GSInfo> data, int selectedPosition) {
            this.data = data;
            this.context = context;
            this.selectedPosition = selectedPosition;
            inflater = LayoutInflater.from(context);
        }

        public void setSelected(int position) {
            this.selectedPosition = position;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }

        @Override
        public Object getItem(int position) {
            return data == null ? null : data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(Rx.layout.kg_dialog_vip_qq_list_item, null);
                holder.gsLayout = convertView.findViewById(Rx.id.gs_qq_item_layout);
                holder.gsTitle = (TextView) convertView.findViewById(Rx.id.gs_tv_title);
                holder.gsQQ = (TextView) convertView.findViewById(Rx.id.gs_tv_qq);
                holder.gsCopyBtn = (Button) convertView.findViewById(Rx.id.gs_btn_copy);
                holder.gsDivider = convertView.findViewById(Rx.id.gs_divider);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final GSInfo info = data.get(position);
            if (info != null) {
                holder.gsTitle.setText(info.getName());
                holder.gsQQ.setText(info.getQq());
                holder.gsCopyBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (context == null) {
                            return;
                        }
                        ClipboardManager cmb = (ClipboardManager) context
                                .getSystemService(Context.CLIPBOARD_SERVICE);
                        cmb.setText(info.getQq().trim());
                        ToastUtil.showMsg("复制成功");
                    }
                });

                holder.gsLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position == selectedPosition) {
                            return;
                        }
                        setSelected(position);
                        loadGsImage(info, position);
                    }
                });

                if (selectedPosition == position) {
                    holder.gsLayout
                            .setBackgroundResource(Rx.drawable.kg_dialog_vip_list_item_selected);
                    holder.gsTitle.setTextColor(Color.parseColor("#ffffff"));
                    holder.gsQQ.setTextColor(Color.parseColor("#ffffff"));
                    holder.gsCopyBtn.setTextColor(context.getResources().getColorStateList(
                            Rx.drawable.kg_selector_text_blue_white));
                    holder.gsCopyBtn.setBackgroundResource(Rx.drawable.kg_selector_bg_white_blue);
                } else {
                    holder.gsLayout.setBackgroundColor(Color.TRANSPARENT);
                    holder.gsTitle.setTextColor(Color.parseColor("#000000"));
                    holder.gsQQ.setTextColor(Color.parseColor("#000000"));
                    holder.gsCopyBtn.setTextColor(context.getResources().getColorStateList(
                            Rx.drawable.kg_selector_text_white_blue));
                    holder.gsCopyBtn.setBackgroundResource(Rx.drawable.kg_selector_bg_blue_white);
                }

                if (position == data.size() - 1) {
                    holder.gsDivider.setVisibility(View.GONE);
                } else {
                    holder.gsDivider.setVisibility(View.VISIBLE);
                }
            }
            return convertView;
        }
    }

    // 如果已经有加载成功过客服图片，则不再显示默认背景
    boolean loadSuccess;

    protected void loadGsImage(GSInfo info, int position) {
        String iconUrl = info.getImgUrl();
        String imgKey = iconUrl + "-" + position;
        String savePath = FolderManager.IMAGE_CACHE_FOLDER + AppUtil.getFileNameFromUrl(iconUrl);
        gsImageGirl.setTag(imgKey);
        Bitmap bitmap = imageLoader.loadImage(context, imgKey, iconUrl, savePath, this);
        if (bitmap == null) {
            if (!loadSuccess) {
                gsImageGirl.setBackgroundResource(Rx.drawable.kg_gs_default_img);
            }
        } else {
            gsImageGirl.setImageBitmap(bitmap);
            loadSuccess = true;
        }
    }

    @Override
    public void imageLoaded(Bitmap bitmap, String key) {
        if (bitmap != null && gsImageGirl != null) {
            gsImageGirl.setImageBitmap(bitmap);
            loadSuccess = true;
        }
    }

    class ViewHolder {
        View gsLayout, gsDivider;

        TextView gsTitle, gsQQ;

        Button gsCopyBtn;
    }
}
